//
//  ViewController.m
//  prefenrence(偏好设置)存储数据
//
//  Created by 伍银荣 on 2021/6/26.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, copy) NSString *name;

@end

@implementation ViewController
- (IBAction)saveBtnClicked:(id)sender {
    /**
     1. 偏好设置不需要关心文件夹和文件的名字(生成的文件名就是Preference文件夹下和bundle Identifier一致的文件名)
     2. 他可以快速创建键值对
     3. 用法和字典基本一样
     */
    self.name = @"ViewController";
    //创建一个NSUserDefaults类型的单例对象
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //使用userDefault可以设置对象的属性
    [userDefault setObject:self.name forKey:@"name"];
    [userDefault setBool:YES forKey:@"isOn"];
    //将文件立即写入磁盘
    [userDefault synchronize];
    NSLog(@"home = %@",NSHomeDirectory());
}

- (IBAction)readBtnClicked:(id)sender {
    NSLog(@"获取数据");
    //创建一个NSUserDefaults类型的单例对象
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //获取值
    NSLog(@"name = %@",[userDefault objectForKey:@"name"]);
    NSLog(@"isOn = %d",[userDefault boolForKey:@"isOn"]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


@end
